home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / jst_dev / sources / src / DiskTools / disk2file.c < prev    next >
C/C++ Source or Header  |  2000-04-12  |  4KB  |  154 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <exec/execbase.h>
  9. #include <exec/libraries.h>
  10. #include <exec/memory.h>
  11. #include <pragmas/dos_pragmas.h>
  12. #include <string.h>
  13.  
  14.  
  15. extern struct ExecBase *SysBase;
  16.  
  17. extern APTR InitTrackDisk(ULONG);
  18. extern void ShutTrackDisk(void);
  19. extern ULONG ReadDosTrack(ULONG,APTR);
  20. extern ULONG CheckDiskIn (void);
  21.  
  22. APTR trackBuffer=0L;
  23. FILE *f=0L;
  24. char *charunit="DF0:";
  25. int allocated=0,diskinit=0;
  26.  
  27. #define READ_DOS_TRACK(trackno) if (ReadDosTrack(trackno,trackBuffer)) printf("Warning: error track %d\n",trackno);
  28.  
  29. void CloseAll(char *mess)
  30.  
  31. {
  32. if (mess!=NULL) printf("** %s\n",mess);
  33.  
  34. if (diskinit) ShutTrackDisk();
  35. if (allocated) Inhibit(charunit,FALSE);
  36. if (trackBuffer!=NULL) FreeMem(trackBuffer,512*11);
  37. if (f!=NULL) fclose(f);
  38. exit(0);
  39. }
  40.  
  41. void BreakHandle(int code)
  42.  
  43. {
  44. CloseAll("User break");
  45. }
  46.  
  47.  
  48. int findArg(unsigned int argc,char ** argv,char * argstr)
  49. {
  50.   int argl,i;
  51.  
  52.   argl=strlen(argstr);
  53.  
  54.   for (i=0;i<argc;i++)
  55.     if (!strncmp(argv[i],argstr,argl)) return i;
  56.  
  57.   return 0;
  58. }
  59.  
  60.  
  61. main(unsigned int argc,char ** argv)
  62.  
  63.  
  64. {
  65.   ULONG i,skiprob=0;
  66.   ULONG diskunit;
  67.   UBYTE *sptr;
  68.   int srarg=0,starttrack=0,endtrack=159,trackok=1;
  69.  
  70.   signal(SIGINT,BreakHandle);
  71.  
  72.   printf("\2331;37;40mDisk2File V1.3\2330;31;40m - a disk image maker by Jean-François Fabre\n");
  73.  
  74.   if (argc<3) {printf("Usage : disk2file <unit number> <filename> [starttrack] [endtrack] <SKIPROB>\n");if (!argc) Delay(200);exit(0);}
  75.  
  76.   if ((argc>3)&&(!strnicmp(argv[3],"SKIPROB",7))) {skiprob=2;srarg=3;}
  77.   if ((argc>4)&&(!strnicmp(argv[4],"SKIPROB",7))) {skiprob=2;srarg=4;}
  78.   if ((argc>5)&&(!strnicmp(argv[5],"SKIPROB",7))) {skiprob=2;srarg=5;}
  79.  
  80.   switch(srarg)
  81.   {
  82.     case 0:
  83.         if (argc>3) starttrack=atoi(argv[3]);
  84.         if (argc>4) endtrack=atoi(argv[4]);
  85.     break;
  86.     
  87.     case 5:
  88.         endtrack=atoi(argv[4]);
  89.     case 4:
  90.         starttrack=atoi(argv[3]);
  91.         break;
  92.  
  93.    }
  94.  
  95.    if (starttrack>endtrack) trackok=0;
  96.    if (starttrack<0) trackok=0;
  97.    if (endtrack>159) trackok=0;
  98.  
  99.   if (!trackok) CloseAll("Bad start/end track settings!");
  100.  
  101.   diskunit=(ULONG)argv[1][0]-'0';
  102.  
  103.   if ((diskunit>3)||(diskunit<0)) {printf("** Unit %d unavailable\n",diskunit);CloseAll(0);}
  104.  
  105.   charunit[2]=diskunit+'0';
  106.  
  107.   if (SysBase->LibNode.lib_Version>36)
  108.     {
  109.     printf("Allocating drive...\n");
  110.     if (Inhibit(charunit,DOSTRUE)==FALSE) CloseAll("Can't allocate device !");
  111.     allocated=1;
  112.     }
  113.  
  114.   if ((ULONG)InitTrackDisk(diskunit)<0) {printf("** Can't open unit %d !",diskunit);CloseAll(0);}
  115.   diskinit=1;
  116.   if (CheckDiskIn()) {printf("** No disk in unit %d !\n",diskunit);CloseAll(NULL);}
  117.  
  118.   f=fopen(argv[2],"wb");
  119.  
  120.   if (f==0L) {printf("Can't create file !\n");CloseAll(0);}
  121.  
  122.   trackBuffer=AllocMem(512*11,MEMF_CHIP|MEMF_CLEAR);  
  123.   if (trackBuffer==0L) CloseAll("Can't allocate memory.");
  124.  
  125.   if (skiprob<starttrack) skiprob=starttrack;
  126.  
  127.   printf("Reading from track %d to track %d (included)...\n",starttrack,endtrack);
  128.  
  129.   if (skiprob && (starttrack<2) )
  130.       {
  131.         /** reads boot block **/
  132.  
  133.         READ_DOS_TRACK(0);
  134.         
  135.         if (fwrite(trackBuffer,1,512*11,f)!=512*11) CloseAll("Disk full !");
  136.         printf("Skipping Track 0 Head 1\n");
  137.  
  138.     sptr=(UBYTE *)trackBuffer;
  139.  
  140.     for (i=0;i<512*11;sptr[i++]=0);
  141.  
  142.         if (fwrite(trackBuffer,1,512*11,f)!=512*11) CloseAll("Disk full !");
  143.       }
  144.  
  145.   for (i=skiprob;i<=endtrack;i++)
  146.       {
  147.         READ_DOS_TRACK(i)
  148.         if (fwrite(trackBuffer,1,512*11,f)!=512*11) CloseAll("Disk full !");
  149.       }
  150.  
  151.   printf("Disk image successfuly written\n");
  152.   CloseAll(NULL);
  153. }
  154.